package com.kinon.ogt.view.activities;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.kinon.ogt.R;
import com.kinon.ogt.view.adapter.FragmentAdapter;
import com.kinon.ogt.view.fragments.MyFarmFragment;
import com.kinon.ogt.view.fragments.UserFragment;
import com.kinon.ogt.view.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 佛祖保佑 永无BUG
 *
 * @author WangZhiYao
 * @date 2018/4/23
 */
public class MainActivity extends AppCompatActivity {

    @BindView(R.id.viewPager)
    NoScrollViewPager mViewPager;
    @BindView(R.id.tv_bottom_farm)
    TextView mTvFarm;
    @BindView(R.id.tv_bottom_user)
    TextView mTvUser;

    private Drawable mTextDrawable;
    private FragmentAdapter mFragmentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initData();
        initView();
    }

    private void initData() {
        List<Fragment> fragments = new ArrayList<>();

        fragments.add(0, new MyFarmFragment());
        fragments.add(1, new UserFragment());

        mFragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments);

        String accessKey = "if9TbJKjKfmJfcfqsy6MmhuDDLetDmwgcAHcj3Ib";
        String secretKey = "YOESNYsRTVxf8Hp0UTOVPzfk1WvMg6UWXiQ1agiB";
    }

    private void initView() {
        mViewPager.setAdapter(mFragmentAdapter);
        mViewPager.setCurrentItem(0);
        setFarmSelected();
        setUserUnselected();
    }

    private void setFarmSelected() {
        mTvFarm.setTextColor(getResources().getColor(R.color.colorGreen));
        mTextDrawable = getResources().getDrawable(R.drawable.ic_farm_selected);
        mTextDrawable.setBounds(0, 0, mTextDrawable.getMinimumWidth(), mTextDrawable.getMinimumHeight());
        mTvFarm.setCompoundDrawables(null, mTextDrawable, null, null);
    }

    private void setFarmUnselected() {
        mTvFarm.setTextColor(getResources().getColor(R.color.colorGray));
        mTextDrawable = getResources().getDrawable(R.drawable.ic_farm_unselected);
        mTextDrawable.setBounds(0, 0, mTextDrawable.getMinimumWidth(), mTextDrawable.getMinimumHeight());
        mTvFarm.setCompoundDrawables(null, mTextDrawable, null, null);
    }

    private void setUserSelected() {
        mTvUser.setTextColor(getResources().getColor(R.color.colorGreen));
        mTextDrawable = getResources().getDrawable(R.drawable.ic_user_selected);
        mTextDrawable.setBounds(0, 0, mTextDrawable.getMinimumWidth(), mTextDrawable.getMinimumHeight());
        mTvUser.setCompoundDrawables(null, mTextDrawable, null, null);
    }

    private void setUserUnselected() {
        mTvUser.setTextColor(getResources().getColor(R.color.colorGray));
        mTextDrawable = getResources().getDrawable(R.drawable.ic_user_unselected);
        mTextDrawable.setBounds(0, 0, mTextDrawable.getMinimumWidth(), mTextDrawable.getMinimumHeight());
        mTvUser.setCompoundDrawables(null, mTextDrawable, null, null);
    }

    @OnClick({R.id.ll_bottom_farm, R.id.ll_bottom_user})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_bottom_farm:
                mViewPager.setCurrentItem(0);
                setFarmSelected();
                setUserUnselected();
                break;
            case R.id.ll_bottom_user:
                mViewPager.setCurrentItem(1);
                setFarmUnselected();
                setUserSelected();
                break;
            default:
                break;
        }
    }
}
